package admin

import (
	"exercise/application/yiran/app/libs"
	"exercise/application/yiran/app/request/adminRequest"
	"exercise/application/yiran/app/service/admin"
	"github.com/gin-gonic/gin"
)

type enrollRuleController struct{}

// List 列表
func (*enrollRuleController) List(ctx *gin.Context) {
	request := new(adminRequest.EnrollRuleListRequest)
	err := libs.BindRequest(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	ret, err := admin.NewEnrollRuleService(ctx).List(request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.Success(ctx, ret)
}

// Add 新增
func (*enrollRuleController) Add(ctx *gin.Context) {
	request := new(adminRequest.AddEnrollRuleRequest)
	err := libs.BindRequest(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	ret, err := admin.NewEnrollRuleService(ctx).Add(request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.Success(ctx, ret)
}

// Edit 编辑
func (*enrollRuleController) Edit(ctx *gin.Context) {
	request := new(adminRequest.EditEnrollRuleRequest)
	err := libs.BindRequest(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	err = admin.NewEnrollRuleService(ctx).Edit(request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.OkToast(ctx, "编辑成功")
}

// Detail 详情
func (*enrollRuleController) Detail(ctx *gin.Context) {
	ret, err := admin.NewEnrollRuleService(ctx).Detail()
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.Success(ctx, ret)
}

// Deleted 删除
func (*enrollRuleController) Deleted(ctx *gin.Context) {
	err := admin.NewEnrollRuleService(ctx).Deleted()
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.OkToast(ctx, "删除成功")
}

func NewEnrollRuleController() *enrollRuleController {
	return &enrollRuleController{}
}
